#coding=utf-8
'''
# @Name:user_resource
# @author:mysouil
# @Description:
# @DATE:2022/12/18 16:39
# @Modified By:
'''

from flask import render_template, request, g, session
from comment.models.student_info import StudentInfo
from comment.utils.decorates import login_required


def valid_login(username, password):
    '''
    校验用户名和密码
    :param username:
    :param password:
    :return:
    '''
    if all([username,password]):
        if username=="admin" and password=="123456":
            students = StudentInfo.query.order_by("id").all()
            session['username'] = username
            session['user_id'] = 0
            return render_template('index.html',**{'students':students,'username':username})
        else:
            return "用户名或密码错误"
    else:
        return '缺少必传参数'


def login_view():
    '''
    登陆
    :return:
    '''
    if request.method == 'GET':
        if not g.username:
            return render_template('login.html')
        else:
            students = StudentInfo.query.order_by("id").all()
            return render_template('index.html', **{'students': students, 'username': g.username})
    else:
        return valid_login(request.form['username'],request.form['password'])




